/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
package com.liferay.analystest.model;

import com.liferay.portal.kernel.util.StringBundler;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Model class for necessary fields that needs to be extracted from
 * log archive file.
 * 
 * @author Dante Wang
 */
public final class Sample  {

	public Sample(String revision, String session, String timestamp)
		throws Exception {

		this(revision, session, timestamp, null);
	}

	public Sample(
			String revision, String session, String timestamp,
			Map<String, String> info)
		throws Exception {

		// revision, session and time cannot be null;
		// revision will be parsed to long and session will be parsed to int;
		// if parsing fails, throw exception.

		_revision = Long.parseLong(Objects.requireNonNull(revision));

		_session = Long.parseLong(Objects.requireNonNull(session));

		_timestamp = Objects.requireNonNull(timestamp);

		_additionalInformation = new HashMap<>();

		if (info != null) {
			setAdditionalInformation(info);
		}

		_name = getRevisionToS().concat("-").concat(_timestamp);
	}

	public void addAdditionalInformation(String key, String value) {
		_additionalInformation.put(key, value);
	}

	public String getAdditionalInformation(String key) {
		return _additionalInformation.get(key);
	}

	public String getName() {
		return _name;
	}

	public long getRevision() {
		return _revision;
	}

	public String getRevisionToS() {
		return String.valueOf(_revision);
	}

	public long getSession() {
		return _session;
	}

	public String getSessionToS() {
		return String.valueOf(_session);
	}

	public String getTimestamp() {
		return _timestamp;
	}

	public void setAdditionalInformation(Map<String, String> info) {
		_additionalInformation.putAll(info);
	}

	@Override
	public String toString() {
		StringBundler sb =
			new StringBundler(_additionalInformation.entrySet().size() * 5 + 8);

		sb.append("Sample Key:");
		sb.append("\n -- Revision: ");
		sb.append(getRevisionToS());

		sb.append("\n -- Date: ");
		sb.append(_timestamp);

		sb.append("\nSession Count: ");
		sb.append(getSessionToS());

		sb.append("\n\nMore Info: \n");

		for (Map.Entry<String, String> entry :
				_additionalInformation.entrySet()) {

			sb.append(" -- ");
			sb.append(entry.getKey());
			sb.append(": ");
			sb.append(entry.getValue());
			sb.append("\n");
		}

		return sb.toString();
	}

	private Map<String, String> _additionalInformation;
	private final String _name;
	private final long _revision;
	private final long _session;
	private final String _timestamp;

}
